﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using cmsservice.Entity;

namespace cmsservice.DAL
{
    public class EMoney_WebDal
    {

        public string GetFreindLink(int ID)
        {
            try
            {
                Tools tool = new Tools();
                tool.AddSqlParameter("@ID", SqlDbType.Int, ID);

                string Query = @" IF EXISTS(SELECT * FROM dbo.Web_Links WITH(NOLOCK) WHERE ID = @ID AND ParentID = -1)
	BEGIN 
			SELECT Show,LMName FROM dbo.Web_Links WITH(NOLOCK) WHERE ID = @ID
			SELECT ID,Show,LMName,LMUrl FROM dbo.Web_Links WITH(NOLOCK) WHERE ParentID = @ID AND Isvalid = 1
	END
ELSE 
	BEGIN
			SELECT 'null','null' 
	END  ";
                DataSet result = SqlHelper.ExecuteGetDataSet(DBConn.EMoney_Web, CommandType.Text, Query, tool.GetSqlParameters());

                if (result.Tables[0].Rows[0][0].ToString() == "null")
                {
                    return string.Empty;
                }
                else
                {
                    //return result.Tables[0];
                    DataRow Row1 = result.Tables[0].Rows[0];
                    DataTable Childs = result.Tables[1];
                    StringBuilder Result = new StringBuilder();
                    Result.Append("[{");
                    Result.AppendFormat("\"Show\":\"{0}\",\"LMName\":\"{1}\",\"list\":[", Row1["Show"].ToString(), Row1["LMName"].ToString());

                    foreach (DataRow Row in Childs.Rows)
                    {
                        Result.Append("{");
                        Result.AppendFormat("\"ID\":\"{0}\",\"Show\":\"{1}\",\"LMName\":\"{2}\",\"LMUrl\":\"{3}\"", Row["ID"].ToString(), Row["Show"].ToString(), Row["LMName"].ToString(), Row["LMUrl"].ToString());
                        Result.Append("},");
                    }
                    string str  = Result.ToString().TrimEnd(',');
                    //Result.Append(str+"]}");

                    return str+"]}]";
                }
                
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }


    }
}
